package com.producer.queue;


import com.producer.queue.bean.DataBean;
import com.producer.queue.define.TaskDefine;
import com.producer.thread.OutPutThread;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * @Author : liu lin qin
 * @Date :  Created by pangoin on 2019/8/26.
 */
public class MainQueue implements Runnable {
    private static final Log logger = LogFactory.getLog(MainQueue.class);
    @Override
    public void run() {
       while (true){
           DataBean dataBean=null;
           try {
               dataBean= TaskDefine.mainQueue.take();
               logger.debug("MainQueue begin queue.");
               if (dataBean == null) {
                   Thread.sleep(2000);// 队列为空，消停一会 // 阻塞队列，这里不生效
                   logger.debug("MainQueue data found in queue continue.");
                   continue;
               }
               do {
                   if(TaskDefine.threadPool.getActiveCount()>200){
                       Thread.sleep(2000);// 队列为空，消停一会 // 阻塞队列，这里不生效
                       logger.debug("MainQueue TaskDefine threadPool ActiveCount >200 sleep 2000");
                       continue;
                   }else{
                       break;
                   }
               }while (true);
               TaskDefine.threadPool.execute(new OutPutThread(dataBean));
           } catch (InterruptedException e) {
               logger.debug("mainQueue  InterruptedException"+e.getMessage());
               if(dataBean!=null)
               TaskDefine.childQueue.add(dataBean);
           }
       }
    }
}
